package com.example.springbootEncrypt.controller;

import com.example.springbootEncrypt.anno.EncryptFilter;
import com.example.springbootEncrypt.dto.PlainResult;
import com.example.springbootEncrypt.dto.UserInfoVO;
import com.example.springbootEncrypt.entity.UserInfo;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

//@Controller 视图解析器可以解析return 的jsp,html页面，并且跳转到相应页面，若返回json等内容到页面，则对应的方法需要加@ResponseBody注解
//@RestController 相当于@Controller+@ResponseBody的结合，返回json数据不需要在方法前面加@ResponseBody注解了，
//但使用@RestController这个注解，方法就不能返回jsp,html页面，视图解析器无法解析jsp,html页面。

@RestController
@RequestMapping("/api")
@Api(tags = "Hello接口")
public class HelloController {

    @ApiOperation(value = "入参和出参都是密文")
    @PostMapping("/both")
    @EncryptFilter
    public PlainResult<UserInfoVO> both(@RequestBody UserInfo userinfo) {
        UserInfoVO userInfoVO = new UserInfoVO();
        userInfoVO.setUsername(userinfo.getUsername() + System.currentTimeMillis());
        userInfoVO.setAge(userinfo.getAge());

        PlainResult<UserInfoVO> result = new PlainResult<>(userInfoVO);
        return result;
    }

    @ApiOperation(value = "入参密文，出参明文")
    @PostMapping("/decryptRequest")
    @EncryptFilter(encryptResponse = false)
    public PlainResult<UserInfoVO> decryptRequest(@RequestBody UserInfo userinfo) {
        UserInfoVO userInfoVO = new UserInfoVO();
        userInfoVO.setUsername(userinfo.getUsername() + System.currentTimeMillis());
        userInfoVO.setAge(userinfo.getAge());

        PlainResult<UserInfoVO> result = new PlainResult<>(userInfoVO);
        return result;
    }

    @ApiOperation(value = "入参明文，出参密文")
    @PostMapping("/encryptResponse")
    @EncryptFilter(decryptRequest = false)
    public PlainResult<UserInfoVO> encryptResponse(@RequestBody UserInfo userinfo) {
        UserInfoVO userInfoVO = new UserInfoVO();
        userInfoVO.setUsername(userinfo.getUsername() + System.currentTimeMillis());
        userInfoVO.setAge(userinfo.getAge());

        PlainResult<UserInfoVO> result = new PlainResult<>(userInfoVO);
        return result;
    }

    @ApiOperation(value = "入参和出参都是明文")
    @PostMapping("/non")
    @EncryptFilter(decryptRequest = false, encryptResponse = false)
    public PlainResult<UserInfoVO> non(@RequestBody UserInfo userinfo) {
        UserInfoVO userInfoVO = new UserInfoVO();
        userInfoVO.setUsername(userinfo.getUsername() + System.currentTimeMillis());
        userInfoVO.setAge(userinfo.getAge());

        PlainResult<UserInfoVO> result = new PlainResult<>(userInfoVO);
        return result;
    }
}
